/*  Tables of what DGP characteristics predict player performance.
*/
								
*Set working directory to rd graph folder:
cd "${main}"

cap log close
log using "${logs}/tables_dgp_prediction.log", replace
								
cap program drop var_prep
program define var_prep

destring dgp_slope_left_of_disc, replace
destring dgp_slope_right_of_disc, replace

gen pos_d = (playerdisc > 0)

gen neg_d = (playerdisc < 0)

gen lhs_pos = (dgp_slope_left_of_disc>=0)

gen rhs_pos = (dgp_slope_right_of_disc>=0)

*Create interaction dummy variables.
gen pos_pos_pos = (lhs_pos == 1 & rhs_pos == 1 & pos_d == 1)

gen pos_pos_neg = (lhs_pos == 1 & rhs_pos == 1 & neg_d == 1)

gen pos_neg_pos = (lhs_pos == 1 & rhs_pos == 0 & pos_d == 1)

gen pos_neg_neg = (lhs_pos == 1 & rhs_pos == 0 & neg_d == 1)

gen neg_pos_pos = (lhs_pos == 0 & rhs_pos == 1 & pos_d == 1)

gen neg_pos_neg = (lhs_pos == 0 & rhs_pos == 1 & neg_d == 1)

gen neg_neg_pos = (lhs_pos == 0 & rhs_pos == 0 & pos_d == 1)

gen neg_neg_neg = (lhs_pos == 0 & rhs_pos == 0 & neg_d == 1)

label variable pos_pos_pos "LHS deriv+, RHS deriv+, discont+"
label variable neg_neg_neg "LHS deriv-, RHS deriv-, discont-"
label variable neg_pos_neg "LHS deriv-, RHS deriv+, discont-"
label variable pos_pos_neg "LHS deriv+, RHS deriv+, discont-"
label variable neg_neg_pos "LHS deriv-, RHS deriv-, discont+"
label variable neg_pos_pos "LHS deriv-, RHS deriv+, discont+"
label variable pos_neg_pos "LHS deriv+, RHS deriv-, discont+"
label variable pos_neg_neg "LHS deriv+, RHS deriv-, discont-"

gen pos_pos = (lhs_pos == 1 & rhs_pos == 1) if playerdisc == 0

gen pos_neg = (lhs_pos == 1 & rhs_pos == 0) if playerdisc == 0

gen neg_neg = (lhs_pos == 0 & rhs_pos == 0) if playerdisc == 0

gen neg_pos = (lhs_pos == 0 & rhs_pos == 1) if playerdisc == 0

label variable pos_pos "LHS deriv+, RHS deriv+"
label variable pos_neg "LHS deriv+, RHS deriv-"
label variable neg_pos "LHS deriv-, RHS deriv+"
label variable neg_neg "LHS deriv-, RHS deriv-"

end

cap file close t
capture erase "${output}/table_dgp_prediction_power.tex"
	file open t using "${output}/table_dgp_prediction_power.tex", write replace

		file write t ///
		"\resizebox*{4.0in}{!}{%"_n ///
		"\begin{tabular*}{1.0\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l c c c c} "_n ///
		" &\multicolumn{4}{c}{} \\"_n ///
		" &    Phase 1 &  Phase 2  & Phase 3 & Phase 4  \\"_n ///
		"\cline{2-5} \\" _n ///

	/*==============================================================================
Phase I Table
-----------------------------------*/
use "${dat}/Pilot 1.0/survey_merged.dta", clear
cap drop _merge

var_prep

tab playerdgp, gen(dgp_)

reg playercorrect pos_d dgp_2 dgp_7 if (dgp_2==1 | dgp_7==1 | dgp_11==1) & playerdisc!=0, vce(cluster pid) 

reg playercorrect pos_d dgp_8 if (dgp_8==1 | dgp_9==1) & playerdisc!=0, vce(cluster pid) 
	
reg playercorrect pos_pos_neg pos_neg_pos neg_neg_pos neg_neg_neg neg_pos_neg dgp_1-dgp_11 if playerdisc != 0, nocons vce(cluster pid) 
reg playercorrect pos_pos_neg pos_neg_neg neg_pos_neg neg_neg_neg dgp_1-dgp_11 if playerdisc != 0, nocons vce(cluster pid) 

foreach var of varlist neg_neg_neg pos_neg_neg neg_pos_neg pos_pos_neg {
local b_`var'_1 = string(_b[`var'], "%10.3fc")
local se_`var'_1 = string(_se[`var'], "%10.3fc")
}

	/*==============================================================================
Phase II Table
-----------------------------------*/
	
use "${dat}/Phase II/survey_merged_PhaseII.dta", clear

var_prep

tab playerdgp, gen(dgp_)

reg playercorrect pos_d dgp_2 dgp_7 if (dgp_2==1 | dgp_7==1 | dgp_11==1) & playerdisc!=0, vce(cluster pid) 

reg playercorrect pos_d dgp_8 if (dgp_8==1 | dgp_9==1) & playerdisc!=0, vce(cluster pid) 

reg playercorrect pos_pos_neg pos_neg_pos neg_neg_pos neg_neg_neg neg_pos_neg dgp_1-dgp_11 if playerdisc != 0, nocons vce(cluster pid) 
reg playercorrect pos_pos_neg pos_neg_neg neg_pos_neg neg_neg_neg dgp_1-dgp_11 if playerdisc != 0, nocons vce(cluster pid) 

foreach var of varlist neg_neg_neg pos_neg_neg neg_pos_neg pos_pos_neg {
local b_`var'_2 = string(_b[`var'], "%10.3fc")
local se_`var'_2 = string(_se[`var'], "%10.3fc")
}
	
/*==============================================================================
Phase III
-------------------------------*/

use "${dat}/Phase III/survey_merged_PhaseIII.dta", clear

var_prep

tab playerdgp, gen(dgp_)

reg playercorrect pos_d dgp_2 dgp_7 if (dgp_2==1 | dgp_7==1 | dgp_11==1) & playerdisc!=0, vce(cluster pid) 

reg playercorrect pos_d dgp_8 if (dgp_8==1 | dgp_9==1) & playerdisc!=0, vce(cluster pid) 
		
reg playercorrect pos_pos_neg pos_neg_pos neg_neg_pos neg_neg_neg neg_pos_neg dgp_1-dgp_11 if playerdisc != 0, nocons vce(cluster pid) 
reg playercorrect pos_pos_neg pos_neg_neg neg_pos_neg neg_neg_neg dgp_1-dgp_11 if playerdisc != 0, nocons vce(cluster pid) 

foreach var of varlist neg_neg_neg pos_neg_neg neg_pos_neg pos_pos_neg {
local b_`var'_3 = string(_b[`var'], "%10.3fc")
local se_`var'_3 = string(_se[`var'], "%10.3fc")
}

/*==============================================================================
Phase VI
-------------------------------*/

use "${dat}/Phase VI/survey_merged_PhaseVI.dta", clear

var_prep

tab playerdgp, gen(dgp_)

reg playercorrect pos_d dgp_2 dgp_7 if (dgp_2==1 | dgp_7==1 | dgp_11==1) & playerdisc!=0, vce(cluster pid) 

reg playercorrect pos_d dgp_8 if (dgp_8==1 | dgp_9==1) & playerdisc!=0, vce(cluster pid) 
		
reg playercorrect pos_pos_neg pos_neg_pos neg_neg_pos neg_neg_neg neg_pos_neg dgp_1-dgp_11 if playerdisc != 0, nocons vce(cluster pid) 
reg playercorrect pos_pos_neg pos_neg_neg neg_pos_neg neg_neg_neg dgp_1-dgp_11 if playerdisc != 0, nocons vce(cluster pid) 

foreach var of varlist neg_neg_neg pos_neg_neg neg_pos_neg pos_pos_neg {
local b_`var'_4 = string(_b[`var'], "%10.3fc")
local se_`var'_4 = string(_se[`var'], "%10.3fc")
}
	
	
foreach var of varlist  neg_neg_neg pos_neg_neg neg_pos_neg pos_pos_neg {

file write t "`: variable label `var''   & `b_`var'_1' & `b_`var'_2' & `b_`var'_3' & `b_`var'_4' \\"_n
file write t "   & (`se_`var'_1') & (`se_`var'_2') & (`se_`var'_3') & (`se_`var'_4') \\"_n

}

	
	file write t  ///
	"\cline{1-5} \\" _n ///
	"\end{tabular*} "_n ///
	"}"_n
	
	file close t
	
*-----------------------------------------------------------
*ZERO DISCONTINUITY:
*----------------------------------------------------------*/

cap file close t2
capture erase "${output}/table_dgp_prediction_size.tex"
	file open t2 using "${output}/table_dgp_prediction_size.tex", write replace

		file write t2 ///
		"\resizebox*{4.0in}{!}{%"_n ///
		"\begin{tabular*}{1.0\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l c c c c} "_n ///
		" &\multicolumn{4}{c}{} \\"_n ///
		" &    Phase 1 &  Phase 2  & Phase 3 & Phase 4   \\"_n ///
		"\cline{2-5} \\" _n ///

use "${dat}/Pilot 1.0/survey_merged.dta", clear
cap drop _merge

var_prep
	
reg y neg_neg pos_neg neg_pos if playerdisc == 0, vce(cluster pid) 

foreach var of varlist  neg_neg pos_neg neg_pos {
local b_`var'_1 = string(_b[`var'], "%10.3fc")
local se_`var'_1 = string(_se[`var'], "%10.3fc")
}
local b_cons_1 = string(_b[_cons], "%10.3fc")
local se_cons_1 = string(_se[_cons], "%10.3fc")
	/*==============================================================================
Phase II Table
-----------------------------------*/
	

use "${dat}/Phase II/survey_merged_PhaseII.dta", clear

var_prep

reg y neg_neg pos_neg neg_pos if playerdisc == 0, vce(cluster pid) 
foreach var of varlist  neg_neg pos_neg neg_pos {
local b_`var'_2 = string(_b[`var'], "%10.3fc")
local se_`var'_2 = string(_se[`var'], "%10.3fc")
}
local b_cons_2 = string(_b[_cons], "%10.3fc")
local se_cons_2 = string(_se[_cons], "%10.3fc")
	

/*==============================================================================
Phase III
-------------------------------*/

use "${dat}/Phase III/survey_merged_PhaseIII.dta", clear

var_prep

reg y neg_neg pos_neg neg_pos if playerdisc == 0, vce(cluster pid) 
foreach var of varlist  neg_neg pos_neg neg_pos {
local b_`var'_3 = string(_b[`var'], "%10.3fc")
local se_`var'_3 = string(_se[`var'], "%10.3fc")
}
local b_cons_3 = string(_b[_cons], "%10.3fc")
local se_cons_3 = string(_se[_cons], "%10.3fc")


/*==============================================================================
Phase VI
-------------------------------*/

use "${dat}/Phase VI/survey_merged_PhaseVI.dta", clear

var_prep

reg y neg_neg pos_neg neg_pos if playerdisc == 0, vce(cluster pid) 
foreach var of varlist  neg_neg pos_neg neg_pos {
local b_`var'_4 = string(_b[`var'], "%10.3fc")
local se_`var'_4 = string(_se[`var'], "%10.3fc")
}
local b_cons_4 = string(_b[_cons], "%10.3fc")
local se_cons_4 = string(_se[_cons], "%10.3fc")

foreach var of varlist neg_neg pos_neg neg_pos {

file write t2 "`: variable label `var''   & `b_`var'_1' & `b_`var'_2' & `b_`var'_3' & `b_`var'_4' \\"_n
file write t2 "   & (`se_`var'_1') & (`se_`var'_2') & (`se_`var'_3')  & (`se_`var'_4') \\"_n

}
file write t2 "LHS deriv+, RHS deriv+ (Mean) & `b_cons_1' & `b_cons_2' & `b_cons_3'  & `b_cons_4' \\"_n
file write t2 "   & (`se_cons_1') & (`se_cons_2') & (`se_cons_3') & (`se_cons_4') \\"_n

	
	file write t2  ///
	"\cline{1-5} \\" _n ///
	"\end{tabular*} "_n ///
	"}"_n
	
	file close t2
	
log close




